<?php
$NOT_INIT=1;

include 'common.php';

//检测新版本
function checkversion($db, $conf){
	$res = file_get_contents('http://paver.zjzd-tech.com/index.php/index/index/getversion');
	$rdata = json_decode($res,true);
	$sql= "select * from appver order by id limit 1";
	$data = $db->select($sql);
	if($rdata['code']==200){
		$vdata= $rdata['data'];
		$filename = explode('/' ,$vdata['url']);
		$filename = end($filename);
		$filename ='/home/wwwroot/paving/public/app/'.$filename;

		if($data){

			$ver = $data[0];
			if($ver['version'] <$rdata['data']['version']){
				$vdata= $rdata['data'];
				$db->insert('appver',$vdata);
				
				$filename = explode(',' ,$vdata['url']);
				$filename = end($filename);
				
			}else{
				echo "\r\nversion same\r\n";
			}
			
		}else{
			echo "download...\r\n";
			down($vdata['url'],$filename);
			sleep(5);
			$fsize = filesize($filename);
			if($fsize<3034951){
				return;
			} 
			
			$vdata['url'] =  str_replace('/home/wwwroot/paving/public','', $filename);
			file_put_contents('log1.txt',json_encode($vdata)."\r\n",FILE_APPEND);
			$db->insert('appver',$vdata);

			echo "\r\nupdate app success\r\n";
		}
	}
	
}

function down($url,$savename){

	exec('sudo /home/wwwroot/pavgps/downapp.sh '.$url);
	/*
	$ch = curl_init();//初始化一个cURL会话
	curl_setopt($ch,CURLOPT_URL,$url);//抓取url
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//是否显示头信息
	curl_setopt($ch,CURLOPT_SSLVERSION,3);//传递一个包含SSL版本的长参数
	$data = curl_exec($ch);// 执行一个cURL会话
	$error = curl_error($ch);//返回一条最近一次cURL操作明确的文本的错误信息。
	curl_close($ch);//关闭一个cURL会话并且释放所有资源
	$destination = $savename;
	$file = fopen($destination,"w+");
	fputs($file,$data);//写入文件
	fclose($file);
	*/
}

while(1){
	checkversion($db,$conf);
	sleep(60);
}



